﻿$(function () {
	$('#manager').datagrid({
		url : 'http://gyp.meidongya.cn/download/statistics',
		fit : true,
		fitColumns : true,
		striped : true,
		rownumbers : true,
		border : false,
		pagination : true,
		nowrap:false,
		pageSize : 25,
		pageNumber : 1,
		pageList : [25, 50, 75,],
		toolbar : '#manager_tool',
		columns : [[
			{
				field : 'ck',
				width : 10,
				checkbox : true,
			},
			{
				field : 'province',
				title : '省份',
				width : 50,
			},
			{
				field : 'code_rate',
				title : '码率',
				width : 20,
				formatter: function(value,row,index){
					if(value==1){
						return '2.5M_VBR';
					} else if(value==2){
						return '2.5M_CBR';
					} else if(value==3){
						return '4M_VBR';
					} else if(value==4){
						return '4M_CBR';
					} else if(value==5){
						return '8M_CBR';
					} else if(value==6){
						return '8M_VBR';
					} else{
						return "未知";
					}
				},
			},
			{
				field : 'program_name',
				title : '节目名',
				width : 50,
			},
			{
				field : 'id',
				title : '编号',
				width : 20,
			},
			{
				field : 'download_status',
				title : '下载状态',
				width : 20,
				styler: function(value,row,index){
					if (row.download_status==2){    
		            return 'color:#448BCD;';    
		        	} 
		        	if (row.download_status==-1){    
		            return 'color:red;';    
	        		}
	        	},
				formatter: function(value,row,index){
					if(value==0){
						return '下载中';
					} else if(value==2){
						return '已下载';
					} else if(value==-1){
						return '失败';
					} else if(value==-3){
						return '放弃';
					} else{
						return "未知";
					}
				},
			},
			{
				field : 'create_time',
				title : '创建时间',
				width : 30,
				formatter: function(value,row,index){
					var date = new Date();
					date.setTime(value * 1000);  
				    var y = date.getFullYear();      
				    var m = date.getMonth() + 1;      
				    m = m < 10 ? ('0' + m) : m;      
				    var d = date.getDate();      
				    d = d < 10 ? ('0' + d) : d;      
				    var h = date.getHours();    
				    h = h < 10 ? ('0' + h) : h;    
				    var minute = date.getMinutes();    
				    var second = date.getSeconds();    
				    minute = minute < 10 ? ('0' + minute) : minute;      
				    second = second < 10 ? ('0' + second) : second;     
				    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second; 
				},
				sortable:true,

			},
			{
				field : 'update_time',
				title : '上传时间',
				width : 30,
				formatter: function(value,row,index){
					var date = new Date();
					date.setTime(value * 1000);  
				    var y = date.getFullYear();      
				    var m = date.getMonth() + 1;      
				    m = m < 10 ? ('0' + m) : m;      
				    var d = date.getDate();      
				    d = d < 10 ? ('0' + d) : d;      
				    var h = date.getHours();    
				    h = h < 10 ? ('0' + h) : h;    
				    var minute = date.getMinutes();    
				    var second = date.getSeconds();    
				    minute = minute < 10 ? ('0' + minute) : minute;      
				    second = second < 10 ? ('0' + second) : second;     
				    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second; 
				},
				sortable:true,

			},
		]], 
	});

	$('#province_name').combotree({
		url : 'http://gyp.meidongya.cn/SysUser/get_canuse_province',
		lines : true,
		/*multiple : false,*/
		/*checkbox : true,*/
		editable: true,
		onlyLeafCheck : true,
		onLoadSuccess : function (node, data) {
			//console.log(data);
			var _this = this;
			if (data) {
				$(data).each(function (index, value) {
					//console.log(value)
					if (this.state == 'closed') {
						$(_this).tree('expandAll');
					}
				});
			}
		},
	});

	manager_tool = {
		search:function(){
	        var check = "";   
	        $("input[name='code_rate']:checked").each(function(){  
	            check += $(this).val()+",";  
	        })  
	        check = check.substring(0,check.length-1); 
			$('#manager').datagrid('load', {
				program_name : $.trim($('input[name="program_name"]').val()),
				province_name : $.trim($('#province_name').combotree('getText')),
				download_status: $('input[name="download_status"]').val(),
				code_rate : check,
			});
		}
	};	
});